package f.h.dragon.reflection.invoker;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * SetFieldInvoker: set-invoker of the Field of java bean
 * 
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/04/24
 * @since 1.0
 */
public class SetFieldInvoker implements Invoker {

	// local variables
	/**
	 * the field of bean
	 */
	private Field field;

	//Constructor
	/**
	 * construct a SetFieldInvoker with field
	 * 
	 * @param field
	 */
	public SetFieldInvoker(Field field) {
		this.field = field;
	}

	//Logic
	/**
	 * invoke: see {@link java.lang.reflect.Field#set(Object)}
	 * 
	 * @param target
	 *            the object the underlying method is invoked from
	 * @param args
	 *            the arguments used for the method call
	 * @return the result of dispatching the method represented by this object
	 *         on <code>target</code> with parameters <code>args</code>
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * 
	 * @see f.h.dragon.reflection.invoker.Invoker#invoke(java.lang.Object, java.lang.Object[])
	 */
	public Object invoke(Object target, Object[] args)
			throws IllegalAccessException, InvocationTargetException {
		field.set(target, args[0]);
		return null;
	}

	/**
	 * the type of method
	 * 
	 * @return
	 * 
	 * @see f.h.dragon.reflection.invoker.Invoker#getType()
	 */
	public Class<?> getType() {
		return field.getType();
	}
}
